﻿using BW.MesProject.Public.Domain.Model;
using BW.MesProject.Public.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace BW.MesProject.Public.Application.DocCode
{
    public class GetDocServices<T> : IGetDocServices<T> where T : class, new()
    {
        private readonly IBaseRepository<DOCCODEMODEL> _docCodeRepository;
        private readonly IBaseRepository<T> _baseRepository;

        public GetDocServices(IBaseRepository<DOCCODEMODEL> docCodeRepository, IBaseRepository<T> baseRepository)
        {
            _docCodeRepository = docCodeRepository;
            _baseRepository = baseRepository;
        }

        public string GetDoCode(string type)
        {
            var list = _docCodeRepository.GetAll().First(x => x.SERVICESNAME == type);
            if(list==null)
            {
                //要在自己的方法中处理未找到单据类型的情况
                return $"获取单据编码{type}失败，请先添加对应的单据类型";
            }
            string code = list.PREFIX + DateTime.Now.ToString(list.RULETYPE);
            string serialnumber = GetSerialNumber(list.DOCUMENTFIEID,list.CODENUM,list.ADDTYPE,list.PREFIX);
            code += serialnumber;
            return code;
        }

        /// <summary>
        /// 获取序列号
        /// </summary>
        /// <param name="DOCUMENTFIEID"></param>
        /// <param name="CODENUM"></param>
        /// <param name="RULETYPE"></param>
        /// <returns></returns>
        private string GetSerialNumber(string DOCUMENTFIEID,decimal? CODENUM,decimal? ADDTYPE,string PREFIX)
        {
            string serialnumber = "1";
            int serialLength = 0;
            var allValues = _baseRepository.GetAll()
                .ToList()
                .Select(x => typeof(T).GetProperty(DOCUMENTFIEID)?.GetValue(x, null)?.ToString() ?? string.Empty)
                .ToList();
            if(!allValues.Any())
            {
                for (int i = 1; i < CODENUM; i++)
                {
                    serialnumber = "0" + serialnumber;
                }
                return serialnumber;
            }
           
            //定义一个变量来存储截取的日期部分
            List<string> day = new List<string>();

            if (ADDTYPE == 1)
            {
                day = allValues.Select(x => x.Substring(PREFIX.Length, 8)).ToList();
                day.Sort((x, y) => int.Parse(x).CompareTo(int.Parse(y)));
                string lastday = day[day.Count - 1];
                string nowday = DateTime.Now.ToString("yyyyMMdd");
                if (lastday == nowday)
                {
                    //截取allValues中每个值的最后CODNUM位
                    var lastFourDigits = allValues.Select(x => x.Length >= CODENUM ? x.Substring(x.Length - (int)CODENUM) : x).ToList();
                    //为lastFourDigits按照数字大小进行排序
                    lastFourDigits.Sort((x, y) => int.Parse(x).CompareTo(int.Parse(y)));
                    int maxValue = Convert.ToInt32(lastFourDigits[lastFourDigits.Count - 1]);
                    maxValue += 1;
                    serialnumber = maxValue.ToString();
                    serialLength = serialnumber.Length;
                    for (int i =0; i < CODENUM - serialLength; i++)
                    {
                        serialnumber = "0" + serialnumber;
                    }
                    return serialnumber;
                }
            }
            else if(ADDTYPE==2)
            {
                day = allValues.Select(x=>x.Substring(PREFIX.Length, 6)).ToList();
                day.Sort((x, y) => int.Parse(x).CompareTo(int.Parse(y)));
                string lastday = day[day.Count - 1];
                string nowday = DateTime.Now.ToString("yyyyMM");
                if (lastday == nowday)
                {
                    //截取allValues中每个值的最后CODNUM位
                    var lastFourDigits = allValues.Select(x => x.Length >= CODENUM ? x.Substring(x.Length - (int)CODENUM) : x).ToList();
                    //为lastFourDigits按照数字大小进行排序
                    lastFourDigits.Sort((x, y) => int.Parse(x).CompareTo(int.Parse(y)));
                    int maxValue = Convert.ToInt32(lastFourDigits[lastFourDigits.Count - 1]);
                    maxValue += 1;
                    serialnumber = maxValue.ToString();
                    serialLength = serialnumber.Length;
                    for (int i = 0; i < CODENUM - serialLength; i++)
                    {
                        serialnumber = "0" + serialnumber;
                    }
                    return serialnumber;
                }
            }
            else if (ADDTYPE==3)
            {
                day = allValues.Select(x => x.Substring(PREFIX.Length, 4)).ToList();
                day.Sort((x, y) => int.Parse(x).CompareTo(int.Parse(y)));
                string lastday = day[day.Count - 1];
                string nowday = DateTime.Now.ToString("yyyy");
                if (lastday == nowday)
                {
                    //截取allValues中每个值的最后CODNUM位
                    var lastFourDigits = allValues.Select(x => x.Length >= CODENUM ? x.Substring(x.Length - (int)CODENUM) : x).ToList();
                    //为lastFourDigits按照数字大小进行排序
                    lastFourDigits.Sort((x, y) => int.Parse(x).CompareTo(int.Parse(y)));
                    int maxValue = Convert.ToInt32(lastFourDigits[lastFourDigits.Count - 1]);
                    maxValue += 1;
                    serialnumber = maxValue.ToString();
                    serialLength = serialnumber.Length;
                    for (int i = 0; i < CODENUM - serialLength; i++)
                    {
                        serialnumber = "0" + serialnumber;
                    }
                    return serialnumber;
                }
            }
            else if (ADDTYPE == 4)
            {
                //截取allValues中每个值的最后CODNUM位
                var lastFourDigits = allValues.Select(x => x.Length >= CODENUM ? x.Substring(x.Length - (int)CODENUM) : x).ToList();
                //为lastFourDigits按照数字大小进行排序
                lastFourDigits.Sort((x, y) => int.Parse(x).CompareTo(int.Parse(y)));
                int maxValue = Convert.ToInt32(lastFourDigits[lastFourDigits.Count - 1]);
                maxValue += 1;
                serialnumber = maxValue.ToString();
                serialLength = serialnumber.Length;
                for (int i = 0; i < CODENUM - serialLength; i++)
                {
                    serialnumber = "0" + serialnumber;
                }
                return serialnumber;
            }

            for (int i = 1; i < CODENUM; i++)
            {
                serialnumber = "0" + serialnumber;
            }
            return serialnumber;
        }
    }
}
